import { Component, EventEmitter, Injector, Output, ViewChild, Input, OnInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import {
    StockZT_BaseInfoServiceProxy,
    EditDtoExtensionsItem,
    Stock_BaseFieldListDto,
    StockZT_SupplierServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import { isUndefined } from 'ngx-bootstrap/chronos/utils/type-checks';
import { isNullOrUndefined } from 'util';
import { CodeItemServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import { element } from '@angular/core/src/render3';
@Component({
    selector: 'createOrEditSupplierModal',
    templateUrl: './create-or-edit-supplier-modal.component.html'
})
export class CreateOrEditSupplierModalComponent extends AppComponentBase implements OnInit {
    constructor(injector: Injector, private _supplierServiceProxy: StockZT_SupplierServiceProxy) {
        super(injector);
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() stocktype;
    active = false;
    saving = false;
    _type;
    selectedValue = new Stock_BaseFieldListDto();
    editDto;
    extensions: Array<EditDtoExtensionsItem>;
    extendextensions: Array<EditDtoExtensionsItem>;
    optionAll;
    itemcode = '';
    ngOnInit(): void { }
    showinfo(id?: string, type?: string): void {
        this.active = true;
        this._type = type;
        switch (this._type) {
            case '工具':
                this._supplierServiceProxy.getSupplierForEditExtension(id).subscribe(result => {
                    let optionAll = {};
                    if (result.selects) {
                        result.selects.forEach(el => {
                            optionAll[el.label] = el.value;
                        });
                    }
                    this.optionAll = optionAll;
                    this.editDto = result.dto;
                    this.extendextensions = result.items;
                    this.editDto.f_SupplierGroup = this._type;
                    this.modal.show();
                });
                break;
        }
    }

    save(): void {
        this.saving = true;
        let t: object = _.filter(this.optionAll.f_SupplierCurrencyOptions, {
            value: this.editDto.f_SupplierCurrency
        });
        this.editDto.f_SupplierCurrency = t[0].label;
        switch (this._type) {
            case '工具':
                this._supplierServiceProxy
                    .createOrUpdateSupplier(this.editDto)
                    .pipe(
                        finalize(() => {
                            this.saving = false;
                        })
                    )
                    .subscribe(() => {
                        this.notify.info(this.l('SavedSuccessfully'));
                        this.close();
                        this.modalSave.emit(null);
                    });
                break;
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
